<?php
/**
 * (jobs) 物流运输
 */
namespace App\Jobs\Basics;

use App\Models\Basics\SysICountry;
use App\Models\Operation\ProfitSku;
use App\Models\Shipping\SSku;
use App\Models\SsBasic\AreaSku;
use App\Models\Stocking\StockingSku;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class TransportRelate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $areaId;
    protected $airFee;
    protected $seaFee;
    protected $airDay;
    protected $seaDay;

    /**
     * TransportRelate constructor.
     * @param $areaId
     * @param $airFee
     * @param $seaFee
     * @param $airDay
     * @param $seaDay
     */
    public function __construct($areaId,$airFee,$seaFee,$airDay,$seaDay)
    {
        $this->areaId = $areaId;
        $this->airFee = $airFee;
        $this->seaFee = $seaFee;
        $this->airDay = $airDay;
        $this->seaDay = $seaDay;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->areaId,$this->airFee,$this->seaFee,$this->airDay,$this->seaDay);
    }

    private function exeFillData($areaId,$airFee,$seaFee,$airDay,$seaDay){
        $this->_sSBasic($areaId,$airDay,$seaDay);
        $this->_profitSku($areaId,$airFee,$seaFee);
    }

    //更新备货，提货
    private function _sSBasic($areaId,$airDay,$seaDay){
        StockingSku::query()->where('area_id',$areaId)->orderBy('id')
            ->chunk(500,function ($item) use ($airDay,$seaDay){
                foreach ($item as $_item){
                    $_item->air_day = $airDay;
                    $_item->air_available = AreaSku::getAvailable($_item->purchase_cycle,$_item->air_day);
                    $_item->air_ship_factor = AreaSku::getShipFactor($_item->air_day);
                    $_item->air_stock_factor = AreaSku::getStockFactor($_item->purchase_cycle,$_item->air_day);
                    $_item->sea_day = $seaDay;
                    $_item->sea_available = AreaSku::getAvailable($_item->purchase_cycle,$_item->sea_day);
                    $_item->sea_ship_factor = AreaSku::getShipFactor($_item->sea_day);
                    $_item->sea_stock_factor = AreaSku::getStockFactor($_item->purchase_cycle,$_item->sea_day);
                    $_item->save();
                    $_item->over_stock = StockingSku::getOverStock($_item);
                    $_item->over_stock_value = StockingSku::getOverStockValue($_item->over_stock,$_item->unit_price);
                    $_item->save();
                }
            });
        usleep(10);
        SSku::query()->where('area_id',$areaId)->orderBy('id')
            ->chunk(500,function ($item) use ($airDay,$seaDay){
                foreach ($item as $_item){
                    $_item->air_day = $airDay;
                    $_item->air_factor = AreaSku::getSSkuFactor($_item->purchase_cycle,$_item->air_day);
                    $_item->sea_day = $seaDay;
                    $_item->sea_factor = AreaSku::getSSkuFactor($_item->purchase_cycle,$_item->sea_day);
                    $_item->save();
                }
            });
    }

    //更新毛利率表
    private function _profitSku($areaId,$airFee,$seaFee){
        $countryIds = SysICountry::getCountryIdsByAreaId($areaId);
        ProfitSku::query()->whereIn('country_id',$countryIds)->orderBy('id')
            ->chunk(500,function ($item) use ($airFee,$seaFee){
                foreach ($item as $_item){
                    $fee = $_item->transport == 1 ? $seaFee : $airFee;
                    $_item->head_cost = round($_item->weight * $fee / $_item->rate_odds,3);
                    $tpl = round($_item->tpl_rmb / $_item->rate_odds,3);
                    $_item->cost = ProfitSku::getCost($tpl,$_item->sale_price,$_item->head_cost,$_item->fba_fee,$_item->commission,$_item->return,$_item->vat);
                    $_item->profit = round($_item->sale_price - $_item->cost,3);
                    $_item->profit_rate = $_item->sale_price == 0 ? 0 : round($_item->profit / $_item->sale_price,3);
                    $_item->save();
                }
            });
    }
}
